﻿using Kanban.Dapper.Dal;
using Kanban.IDal;
using Kanban.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Kanban.Dal
{
    public class WarehouseWarnDal : BaseDal<WarehouseWarn>, IWarehouseWarnDal
    {
        public WarehouseWarnDal()
        {
            TableName = "WarehouseWarn";
        }

        public async Task<bool> Exists(WarehouseWarn model)
        {
            string sql = "SELECT Id FROM dbo.WarehouseWarn with(NOLOCK)  WHERE PartNo = @PartNo AND RePartNo = @RePartNo";
            var result = await _db.GetAsync(sql, model);
            return result != null;
        }

        public Task<Tuple<IList<object>, int>> GetModelsByPageAsync(string search, int pageSize, int pageIndex)
        {
            string whereSql = "1 = 1";
            if (!string.IsNullOrEmpty(search))
            {
                whereSql += $" AND (PartNo LIKE '%{search}%' or RePartNo LIKE '%{search}%' or StoreCode LIKE '%{search}%')";
            }
            return GetModelsByPageAsync("*", whereSql, "Id DESC", pageSize, pageIndex);
        }

        public async Task<int> BatchAdd(List<WarehouseWarn> list)
        {
            string paras = _db.GetParas<WarehouseWarn>();
            string sql = $"insert into WarehouseWarn({paras}) values({_db.GetATParas<WarehouseWarn>()})";
            var result = await _db.ExecuteAsync(sql, list);
            return result;
        }
    }
}
